package com.wang.extension;

import com.wang.domain.WlyBeanDefinition;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.stereotype.Component;

/**
 * Bean注册扩展点，可以动态的注册Bean
 *
 * @Author wly
 * @Date 2018/7/11 14:22
 */
@Component
public class TestBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
    /**
     * 注册一个Bean到spring容器中
     *
     * @param registry
     * @throws BeansException
     */
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        for (int i = 0; i < 5; i++) {
            // bean的名称
            String beanName = "wly" + i;
            // bean的定义
            BeanDefinitionBuilder bdb = BeanDefinitionBuilder.rootBeanDefinition(WlyBeanDefinition.class);
            registry.registerBeanDefinition(beanName, bdb.getBeanDefinition());
        }

    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    }
}
